<?php

namespace Admin\Controller;
use Think\Controller;

class AdminUserController extends Controller{

	/*
		新增后台用户
	*/
	public function add(){

		$this->isAjaxLogin();//检查是否登录


		$name='fhreeufhr';//I('post.name');//帐号
		$password='123456';//I('post.password');//密码
		$cpassword='123456';//I('post.password');//确认密码
		$phone='2347483222';//I('post.skimp');//手机号
		$sign=1;//1表示未锁定，0表示锁定
		

		$data['name']   = $name;
        $data['password']   = md5($password); 
        $data['phone']   = $phone;
        $data['cpassword']=md5($cpassword);
        $data['sign']=$sign;

		$model=D('admin_user');


        if($model->create($data) &&$model->add($data)){
        	$ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
        	$ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  $model->getError();
        }
        
        $this->ajaxReturn($ajaxReturn);
	}

	/*
	删除用户
	*/

	public function delete(){

		$this->isAjaxLogin();//检查是否登录

        $id = I('get.id');

        $userid = explode(',',$id);

        $model = M('admin_user');

        $data['status'] = 1;

        

        foreach($userid as $v){
            $model->where('id='.$v)->save($data);
        }

        
        
        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);
	}

	/*
    修改用户
    */
    public function update(){
        $this->isAjaxLogin();//检查是否登录

        $model=M('admin_user');

        $name=I('post.name');//用户名
        //$password=I('post.password');//密码
        $phone=I('post.key');//手机号

        $id=I('post.id');

        $data['name']   = $name;
        //$data['password']   = $password;
        $data['phone']   = $phone;
        if($model->validate($model->_update_validate)->create($data) &&$model->where('id='.$id)->save($data)){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  $model->getError();
        }
        $this->ajaxReturn($ajaxReturn);
    }

   /*
    详情
    */
    public  function infos(){
        $this->isAjaxLogin();

        $id = I('post.id/d');

        $Model = M('admin_user');

        $result = $Model->where([
                'id'=>$id,
            ])->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }



	/*
	后台用户列表
	*/

	public function lists(){
		$this->isAjaxLogin();//检查是否登录

        $model = M('admin_user');

        $result = $model->order('id desc')->select();

        if($result !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

    //商务管理
    public function commerce(){

        $page       = I('post.page');
        $page_size  = I('post.page_size');

        $user = M("admin_user");

        $count = $user
               ->join("table_user_role on table_admin_user.id=table_user_role.uid")
               ->join("table_role on table_user_role.rid=table_role.id")
               ->where("table_role.uname='商务' and status=0")->count();

        $total      =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $res  = $user
              ->order("table_admin_user.id desc")
              ->join("table_user_role on table_admin_user.id=table_user_role.uid")
              ->join("table_role on table_user_role.rid=table_role.id")
              ->field("table_admin_user.id,table_admin_user.name,table_admin_user.phone,table_admin_user.sign")
              ->limit($limitStart,$page_size)
              ->where("table_role.uname='商务' and status=0")
              ->select();

            $data['page']['total_page'] = $total;
            $data['list'] = $res;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
      

        $this->ajaxReturn($ajaxReturn);


    }

    //媒介管理
    public function medium(){
        $page       = I('post.page');
        $page_size  = I('post.page_size');

        $user = M("admin_user");

        $count = $user
               ->order("table_admin_user.id desc")
               ->join("table_user_role on table_admin_user.id=table_user_role.uid")
               ->join("table_role on table_user_role.rid=table_role.id")
               ->where("table_role.uname='媒介' and status=0")
               ->count();

        $total      =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $res  = $user
              ->order("table_admin_user.id desc")
              ->join("table_user_role on table_admin_user.id=table_user_role.uid")
              ->join("table_role on table_user_role.rid=table_role.id")
              ->field("table_admin_user.id,table_admin_user.name,table_admin_user.phone,table_admin_user.sign")
              ->limit($limitStart,$page_size)
              ->where("table_role.uname='媒介' and status=0")
              ->select();

            $data['page']['total_page'] = $total;
            $data['list'] = $res;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        

        $this->ajaxReturn($ajaxReturn);
    }

    //商务封停修改状态
    public function commerce_state(){
        $id     = I('post.id');
        $status = I('post.status');
        $user   = M('admin_user');
        $data['sign'] = $status;
        $res    = $user->where("id=$id")->save($data);
        if($res){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = '操作成功！';
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = '操作失败！';
        }
        $this->ajaxReturn($ajaxReturn);
    }

    //商务，渠道用户删除
    public function  user_delete(){
        $id  = I('post.id');
        $res = M('admin_user')->where("id=$id")->save("status=1");
        if($res){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = '删除成功！';
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = '删除失败！';
        }
        $this->ajaxReturn($ajaxReturn);
    }

    //商务，渠道用户修改
    public function  user_update(){
        $id       = I('psot.id');
        $name     = I('psot.name');
        $phone = I('post.phone');

        $data['name']  = $name;
        $data['phone'] = $phone;

        $res = M('admin_user')->where("id=$id")->save($data);
        if($res){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = '删除成功！';
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = '删除失败！';
        }
        $this->ajaxReturn($ajaxReturn);

    }

    public function admin_add(){
        $data['name'] = I('post.name');
        $data['phone'] = I('post.key');
        $data['password'] = md5('123456');

        if($data['name']==false){
            $ajaxReturn['code'] = 3;
            $ajaxReturn['msg']  = '缺少用户名';
            $this->ajaxReturn($ajaxReturn);
        }

        if($data['phone']==false){
            $ajaxReturn['code'] = 4;
            $ajaxReturn['msg']  = '缺少手机号';
            $this->ajaxReturn($ajaxReturn);
        }

        $admin_user = M('admin_user');
        $user = $admin_user->add($data);
        $role = M('role')->where("uname='商务'")->find();

        $arr['uid'] = $user;
        $arr['rid'] = $role['id'];
        $result = M('user_role')->add($arr);
        if($result){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = '操作成功！';
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = '操作失败！';
        }

        $this->ajaxReturn($ajaxReturn);

    }

    public function media_add(){
        $data['name'] = I('post.name');
        $data['phone'] = I('post.key');
        $data['password'] = md5('123456');

        if($data['name']==false){
            $ajaxReturn['code'] = 3;
            $ajaxReturn['msg']  = '缺少用户名';
            $this->ajaxReturn($ajaxReturn);
        }

        if($data['phone']==false){
            $ajaxReturn['code'] = 4;
            $ajaxReturn['msg']  = '缺少手机号';
            $this->ajaxReturn($ajaxReturn);
        }

        $admin_user = M('admin_user');
        $user = $admin_user->add($data);
        $role = M('role')->where("uname='媒介'")->find();
        // print_r($role);die;
        $arr['uid'] = $user;
        $arr['rid'] = $role['id'];
        $result = M('user_role')->add($arr);
        if($result){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = '操作成功！';
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = '操作失败！';
        }

        $this->ajaxReturn($ajaxReturn);

    }


}